package com.csu.letaobackend.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.csu.letaobackend.bean.Item;
import com.csu.letaobackend.bean.Specialoffer;
import com.csu.letaobackend.bean.Swiperitem;
import com.csu.letaobackend.bean.User;
import com.csu.letaobackend.commom.CommonResponse;
import com.csu.letaobackend.dto.ItemVO2;
import com.csu.letaobackend.service.CatalogService;
import com.csu.letaobackend.vo.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@CrossOrigin
@RequestMapping("/catalog")
public class CatalogController {

      @Autowired
      CatalogService catalogService;

      @GetMapping("/swiper-items")
      @ResponseBody
      public CommonResponse<List<SwiperitemVO>> getSwiperitems() {
            return catalogService.getSwiperitems();
      }

      @GetMapping("/categories")
      @ResponseBody
      public CommonResponse<List<CategoryVO>> getCategories() {

            return catalogService.getCategories();
      }

      @GetMapping("/categories/{id}")
      @ResponseBody
      public CommonResponse<List<ProductVO>> getProductsByCategory(@PathVariable int id) {
            return catalogService.getProductsByCategoryId(id);
      }

      @GetMapping("/categories/products/{id}")
      @ResponseBody
      public CommonResponse<List<ItemVO>> getItemsByProduct(@PathVariable int id, @RequestParam int sortby, HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            String username;
            if (login_user == null)
                  username = null;
            else
                  username = login_user.getUsername();
            return catalogService.getItemsByProductId(id, sortby, username);
      }

      @GetMapping("/categories/products/{id}/pages/{pagesize}/{pageid}")
      @ResponseBody
      public CommonResponse<IPage<ItemVO>> getItemsPageByProduct(@PathVariable int id, @RequestParam int sortby, @PathVariable int pagesize, @PathVariable int pageid, HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            String username;
            if (login_user == null)
                  username = null;
            else
                  username = login_user.getUsername();
            return catalogService.getItemsPages(id, sortby, pageid, pagesize, username);
      }

      @GetMapping("/items/{itemid}")
      @ResponseBody
      public CommonResponse<ItemVO> getItem(@PathVariable int itemid, HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            String username;
            if (login_user == null)
                  username = null;
            else
                  username = login_user.getUsername();
            return catalogService.getItem(itemid, username);

      }

      @GetMapping("/items/{itemid}/attributes")
      @ResponseBody
      public CommonResponse<List<ItemAttrVO>> getItemAttrByItemId(@PathVariable int itemid) {

            return catalogService.getAttrByItemId(itemid);
      }

      @GetMapping("/items/shops/{shopid}")
      @ResponseBody
      public CommonResponse<List<ItemVO>> getItemsByShopId(@PathVariable int shopid, @RequestParam int sortby, HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            String username;
            if (login_user == null)
                  username = null;
            else
                  username = login_user.getUsername();
            return catalogService.getItemsByShopId(shopid, sortby, username);
      }

      @GetMapping("/items/shops/{shopid}/pages/{pagesize}/{pageid}")
      @ResponseBody
      public CommonResponse<IPage<ItemVO>> getItemsPageByShopId(@PathVariable int shopid, @RequestParam int sortby, @PathVariable int pagesize, @PathVariable int pageid, HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            String username;
            if (login_user == null)
                  username = null;
            else
                  username = login_user.getUsername();
            return catalogService.getItemsPagesByShopId(shopid, sortby, pageid, pagesize, username);
      }

      @GetMapping("/items/query/pages/{pagesize}/{pageid}")
      @ResponseBody
      public CommonResponse<IPage<ItemVO>> searchItems(@RequestParam String keyword, @RequestParam int categoryId,
                                                       @RequestParam int sortby, @PathVariable int pagesize,
                                                       @PathVariable int pageid, HttpSession httpSession) {
            //<categoryid>为0表示在所有商品中搜索
            User login_user = (User) httpSession.getAttribute("login_user");
            String username;
            if (login_user == null)
                  username = null;
            else
                  username = login_user.getUsername();
            return catalogService.searchItemsPages(keyword, categoryId, sortby, pageid, pagesize, username);
      }

      @GetMapping("/items/popular")
      @ResponseBody
      public CommonResponse<List<ItemVO>> searchItems(@RequestParam int num) {

            return catalogService.getPopularItems(num);
      }

      @PostMapping("/items/{itemid}/stock")
      @ResponseBody
      public CommonResponse<ItemVO> updateStockByItemId(@PathVariable int itemid, @RequestParam int stock) {
            return catalogService.updateStockByItemId(itemid, stock);
      }

      @PostMapping("/items/{itemid}/price")
      @ResponseBody
      public CommonResponse<ItemVO> updatePriceByItemId(@PathVariable int itemid, @RequestParam int price) {
            return catalogService.updatePriceByItemId(itemid, price);
      }

      @PostMapping("/items/{itemid}/rate")
      @ResponseBody
      public CommonResponse<Specialoffer> updateSpecialRateByItemId(@PathVariable int itemid, @RequestParam double rate) {
            return catalogService.updateSpecialRateByItemId(itemid, rate);
      }


      @GetMapping("/records")
      @ResponseBody
      public CommonResponse<List<BrowsingrecordVO>> getRecords(HttpSession httpSession) {
            User login_user = (User) httpSession.getAttribute("login_user");
            return catalogService.getRecords(login_user.getUsername());
      }

      @PostMapping("/records")
      @ResponseBody
      public CommonResponse<BrowsingrecordVO> insertRecords(HttpSession httpSession, int itemid) {
            User login_user = (User) httpSession.getAttribute("login_user");
            if (login_user != null) {
                  return catalogService.insertRecord(itemid, login_user.getUsername());
            } else {
                  return catalogService.insertRecord(itemid, "hsj");
            }
      }


      @PutMapping("/items/{itemid}")
      @ResponseBody
      public CommonResponse updateitem(@PathVariable int itemid, @RequestParam int stock,
                                       @RequestParam int price, @RequestParam String description) {
            return catalogService.updateitem(itemid, stock, price, description);
      }

      @PutMapping("/items/{itemid}/available")
      @ResponseBody
      public CommonResponse<ItemVO> updateByItemId(@PathVariable int itemid, @RequestParam int available) {
            return catalogService.updateAvailableByItemId(itemid, available);
      }


      @PostMapping("/items")
      @ResponseBody
      public CommonResponse additem(ItemVO2 itemVO2) {
            return catalogService.additem(itemVO2);
      }


}

